"""
系统配置文件，用于存储全局设置
"""
import os
from pathlib import Path
from dotenv import load_dotenv

# 加载环境变量
env_path = Path(__file__).parent.parent / '.env'
load_dotenv(dotenv_path=env_path)

# 兼容OpenAI接口的配置
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
OPENAI_MODEL = os.getenv("OPENAI_MODEL")
OPENAI_API_BASE = os.getenv("OPENAI_API_BASE")

# 数据源配置
# 数据源优先级 (1:最高)
DATA_SOURCE_PRIORITY = os.getenv("DATA_SOURCE_PRIORITY", "akshare,tushare").split(',')

# AKShare配置
AKSHARE_COOKIES = os.getenv("AKSHARE_COOKIES")
TDX_HOST = os.getenv("TDX_HOST", "119.147.212.81")
TDX_PORT = int(os.getenv("TDX_PORT", "7709"))

# Tushare配置
TUSHARE_TOKEN = os.getenv("TUSHARE_TOKEN")
TUSHARE_ENDPOINT = os.getenv("TUSHARE_ENDPOINT", "http://api.tushare.pro")

# 数据库配置
# 数据库类型: 'postgresql' 或 'mongodb' (当前迁移到 postgresql)
DB_TYPE = os.getenv("DB_TYPE", "postgresql")
DB_HOST = os.getenv("DB_HOST", "localhost")
DB_PORT = os.getenv("DB_PORT", "5432")
DB_NAME = os.getenv("DB_NAME", "investment_data")
DB_USER = os.getenv("DB_USER", "postgres")
DB_PASSWORD = os.getenv("DB_PASSWORD", "123456")  # 默认密码与.env文件一致

# 构建 SQLAlchemy 连接字符串
# 注意：确保密码中的特殊字符已进行 URL 编码（如果直接放入 URL）
# 更推荐的方式是在应用代码中处理密码，或使用 SQLAlchemy 的 URL 对象
if not all([DB_TYPE, DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD]):
    raise ValueError("数据库配置不完整，请检查环境变量设置")
DATABASE_URL = f"{DB_TYPE}+psycopg2://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"

# 日志配置
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")

# A股市场配置
MARKET_OPEN_TIME = os.getenv("MARKET_OPEN_TIME", "09:30:00")
MARKET_CLOSE_TIME = os.getenv("MARKET_CLOSE_TIME", "15:00:00")
MARKET_BREAK_START = os.getenv("MARKET_BREAK_START", "11:30:00")
MARKET_BREAK_END = os.getenv("MARKET_BREAK_END", "13:00:00")
TRADE_DATES_BUFFER = int(os.getenv("TRADE_DATES_BUFFER", "60"))  # 保存最近60个交易日信息
MARKET_INDEX_CODE = os.getenv("MARKET_INDEX_CODE", "000001")  # 默认上证指数代码

# 回测配置
BACKTEST_START_DATE = os.getenv("BACKTEST_START_DATE", "2023-01-01")
BACKTEST_END_DATE = os.getenv("BACKTEST_END_DATE", "2023-12-31")
INITIAL_CAPITAL = float(os.getenv("INITIAL_CAPITAL", "1000000.0"))  # 初始资金100万

# 智能体配置
AGENT_TEMPERATURE = float(os.getenv("AGENT_TEMPERATURE", "0"))
AGENT_MAX_TOKENS = int(os.getenv("AGENT_MAX_TOKENS", "4096"))
AGENT_MEMORY_SIZE = int(os.getenv("AGENT_MEMORY_SIZE", "10"))  # 智能体记忆最近10次交互

# 策略配置
DEFAULT_POSITION_SIZE = float(os.getenv("DEFAULT_POSITION_SIZE", "0.1"))  # 默认仓位大小
MAX_POSITION_PER_STOCK = float(os.getenv("MAX_POSITION_PER_STOCK", "0.3"))  # 单只股票最大仓位
STOP_LOSS_THRESHOLD = float(os.getenv("STOP_LOSS_THRESHOLD", "0.05"))  # 止损阈值
TAKE_PROFIT_THRESHOLD = float(os.getenv("TAKE_PROFIT_THRESHOLD", "0.2"))  # 止盈阈值

# 目标股票配置
TARGET_STOCKS = os.getenv("TARGET_STOCKS", "600519,000858,600036,000333,601318").split(",")

# 行业配置
SECTOR_ALLOCATION = {
    "tech": float(os.getenv("TECH_SECTOR_WEIGHT", "0.3")),
    "consumer": float(os.getenv("CONSUMER_SECTOR_WEIGHT", "0.2")),
    "healthcare": float(os.getenv("HEALTHCARE_SECTOR_WEIGHT", "0.15")),
    "finance": float(os.getenv("FINANCE_SECTOR_WEIGHT", "0.15")),
    "others": float(os.getenv("OTHERS_SECTOR_WEIGHT", "0.2"))
}

# 资产配置
DEFAULT_ASSET_ALLOCATION = {
    "股票": float(os.getenv("STOCK_ALLOCATION", "0.7")),
    "债券": float(os.getenv("BOND_ALLOCATION", "0.2")),
    "现金": float(os.getenv("CASH_ALLOCATION", "0.1"))
}

# 行业中文名称映射
SECTOR_NAME_MAP = {
    "tech": "科技",
    "consumer": "消费",
    "healthcare": "医疗",
    "finance": "金融",
    "others": "其他"
}

# 数据缓存配置
DATA_CACHE_DIR = os.getenv("DATA_CACHE_DIR", "data/cache")

# 智能体人员配置
AGENT_CONFIG = {
    "market_analyst": {
        "name": os.getenv("MARKET_ANALYST_NAME", "张明睿"),
        "role": os.getenv("MARKET_ANALYST_ROLE", "首席市场分析师")
    },
    "stock_analyst": {
        "name": os.getenv("STOCK_ANALYST_NAME", "李芳华"),
        "role": os.getenv("STOCK_ANALYST_ROLE", "高级个股分析师")
    },
    "chief_investment_officer": {
        "name": os.getenv("CIO_NAME", "陈远山"),
        "role": os.getenv("CIO_ROLE", "首席投资官")
    }
}

# 宏观经济数据源配置
MACRO_DATA_SOURCE = os.getenv("MACRO_DATA_SOURCE", "akshare") # 默认使用 akshare